﻿using Microsoft.AspNetCore.Mvc;
using STF.Blog.IService;
using STF.Blog.WebApp.Areas.ForumManage.Models.Users;
using X.PagedList;
using STF.Blog.Common;
using STF.Blog.WebApp.Areas.ForumManage.Filters;

namespace STF.Blog.WebApp.Areas.ForumManage.Controllers
{
    [Area("ForumManage")]
    [Login]
    public class UsersController : Controller
    {
        private IUsersService _ius;
        public UsersController(IUsersService ius)
        {
            _ius = ius;
        }

        [HttpGet]
        public async Task<IActionResult> Index(string search , int page = 1)
        {
            search = string.IsNullOrEmpty(search) ? "" : search;
            var data = await _ius.GetByNameAsync(search);

            var UsersList = data.Select(us => new UsersListViewModel 
            {
                Id = us.Id,
                Email = us.Email,
                Pwd = us.Pwd,
                NickName = us.NickName,
                StateCode = us.StateCode,
                CreateTime = us.CreateTime,
                UpdateTime = us.UpdateTime
            });

            ViewBag.Search = search;
            var pages = await UsersList.ToPagedListAsync(page,PageConfig.PageSize);
            return View(pages);
        }

        [HttpPost]
        public async Task<IActionResult> Delete(int id) 
        {
            if (ModelState.IsValid) 
            {
                await _ius.PutTrashAsync(id);
                return Redirect("~/ForumManage/Users/Index");
            }
            return View();
        }
    }
}
